<template>
  <nav class="navbar">
    <div class="nav-container">
      <div class="nav-logo">
        <h2>招聘系统</h2>
      </div>
      <ul class="nav-menu" v-if="isAuthenticatedComputed">
        <li v-if="isRecruiterComputed" class="nav-item">
          <router-link to="/post-job" class="nav-link">发布职位</router-link>
        </li>
        <li v-if="isRecruiterComputed" class="nav-item">
          <router-link to="/my-jobs" class="nav-link">我的职位</router-link>
        </li>
        <li v-if="isApplicantComputed" class="nav-item">
          <router-link to="/jobs" class="nav-link">职位列表</router-link>
        </li>
        <li v-if="isApplicantComputed" class="nav-item">
          <router-link to="/my-applications" class="nav-link">我的申请</router-link>
        </li>
        <li class="nav-item">
          <button @click="handleLogout" class="nav-link logout-btn">退出登录</button>
        </li>
      </ul>
      <ul class="nav-menu" v-else>
        <li class="nav-item">
          <router-link to="/login" class="nav-link">登录</router-link>
        </li>
        <li class="nav-item">
          <router-link to="/register" class="nav-link">注册</router-link>
        </li>
      </ul>
    </div>
  </nav>
</template>

<script setup>
import { computed } from 'vue';
import { useRouter } from 'vue-router';
import { getCurrentUser, isAuthenticated, isRecruiter, isApplicant } from '../services/authService';
import { logout } from '../services/authService';

const router = useRouter();

// 计算属性
const currentUser = computed(() => getCurrentUser());
const isAuthenticatedComputed = computed(() => isAuthenticated());
const isRecruiterComputed = computed(() => isRecruiter());
const isApplicantComputed = computed(() => isApplicant());

// 方法
const handleLogout = () => {
  logout();
  router.push('/login');
};
</script>

<style scoped>
.navbar {
  background-color: #2c3e50;
  color: white;
  padding: 1rem 0;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.nav-container {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.nav-logo h2 {
  margin: 0;
  color: white;
}

.nav-menu {
  display: flex;
  list-style: none;
}

.nav-item {
  margin-left: 20px;
}

.nav-link {
  color: white;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1rem;
}

.nav-link:hover {
  color: #3498db;
}

.logout-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1rem;
}
</style>